package com.goodedeeds.webapp.controller;

import com.goodedeeds.domain.IResourcePage;
import com.goodedeeds.domain.impl.db.DomainFactory;
import com.youtills.core.lang.NumberUtil;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: rchiluve
 * Date: 2/23/14
 * Time: 8:15 PM
 * To change this template use File | Settings | File Templates.
 */
@UrlBinding(ResourcePageAction.URL)
public class ResourcePageAction extends BaseDashboardAction {

    public static final String URL = "/resource-page.action";

    private static final String VIEW = JSP_ROOT + "/resource-page-view.jsp";
    private static final String EDIT_VIEW = JSP_ROOT + "/resource-page-edit.jsp";
    private static final String LIST_VIEW = JSP_ROOT + "/resource-page-list.jsp";
    private static final String SHOW_VIEW = JSP_ROOT + "/resource-page-show.jsp";

    private Long pageResourceId;
    private Long pageResourceType;
    private Long pageId;
    private Integer pageType = 1;

    private IResourcePage resourcePage;

    private String dashboardContent;

    @Override
    public String getDashboardContent() {
        return dashboardContent;
    }

    @Override
    public DashboardTab getCurrentTab() {
        return DashboardTab.NONE;
    }

    public IResourcePage getResourcePage() {
        if(resourcePage == null){
            if(getPageId() != null){
                resourcePage = DomainFactory.getResourcePageDao().getResourcePage(getActionContext(), getPageId().intValue());
            } else {
                resourcePage = DomainFactory.getResourcePageDao().getResourcePage(getActionContext(), getPageResourceType().intValue(), getPageResourceId().intValue(), getPageType().intValue());
            }
        }
        return resourcePage;
    }

    public Long getPageResourceId() {
        if(pageResourceId == null){
            pageResourceId = NumberUtil.parseLong(getRequestParameter("pageResourceId"));
        }
        return pageResourceId;
    }

    public void setPageResourceId(Long pageResourceId) {
        this.pageResourceId = pageResourceId;
    }

    public Long getPageResourceType() {
        if(pageResourceType == null){
            pageResourceType = NumberUtil.parseLong(getRequestParameter("pageResourceType"));
        }
        return pageResourceType;
    }

    public void setPageResourceType(Long pageResourceType) {
        this.pageResourceType = pageResourceType;
    }

    public Long getPageId() {
        if(pageId == null){
            pageId = NumberUtil.parseLong(getRequestParameter("pageId"));
        }
        return pageId;
    }

    public void setPageId(Long pageId) {
        this.pageId = pageId;
    }

    public Integer getPageType() {
        return pageType;
    }

    public void setPageType(Integer pageType) {
        this.pageType = pageType;
    }

    public Resolution save(){
        DomainFactory.getResourcePageDao().saveResourcePage(getActionContext(), resourcePage);
        pageId = resourcePage.getPageId();
        RedirectResolution res = new RedirectResolution(ResourcePageAction.class, "view");
        res.addParameter("pageId", pageId);
        return res;
    }

    private List<IResourcePage> resourcePages;
    public List<IResourcePage> getResourcePages(){
        if(resourcePages == null){
            resourcePages = DomainFactory.getResourcePageDao().getAllResourcePages(getActionContext(), getPageResourceType().intValue(), getPageResourceId().intValue());
        }
        return resourcePages;
    }


    public Resolution list(){
        dashboardContent = LIST_VIEW;
        return showPage();
    }

    public Resolution view(){
        dashboardContent = VIEW;
        return showPage();
    }

    public Resolution edit() {
        dashboardContent = EDIT_VIEW;
        return showPage();
    }

    public Resolution show(){
        dashboardContent = SHOW_VIEW;
        headerContent = getResourcePage().getPageHeader();
        return showPage();
    }

    private String headerContent;

    public String getHeaderContent(){
        if(headerContent != null){
            return headerContent;
        }
        return super.getHeaderContent();
    }
}
